{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 447,
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "import yaml\n",
    "import os\n",
    "from pathlib import Path\n",
    "import datetime\n",
    "import time\n",
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 448,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "yaml_name = \"pict_info.yaml\"\n",
    "pwd = Path(os.path.split(os.path.realpath(__file__))[0])\n",
    "path_data = pwd.joinpath(\"data\")\n",
    "path_sexypict = path_data.joinpath(\"sexypict\")\n",
    "path_yaml = path_sexypict.joinpath(yaml_name)\n",
    "yaml_data: dict\n",
    "yaml_title = dict({\n",
    "    \"name\": \"涩图库配置文件\",\n",
    "    \"__version__\": \"0.0.8\",\n",
    "    \"time\": {\"create_time\": \"\", \"edit_time\": \"\", },\n",
    "    \"dir\": {\"common_dir\": [], \"special_dir\": []},\n",
    "    \"data\": {}\n",
    "})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 449,
   "outputs": [],
   "source": [
    "\n",
    "def yaml_create():\n",
    "    yamlfile = path_yaml\n",
    "    with open(yamlfile, \"w+\", encoding='utf-8') as f:\n",
    "        yaml_title[\"time\"][\"create_time\"] = datetime.datetime.now().__str__()\n",
    "        yaml.dump(yaml_title, f, allow_unicode=True, sort_keys=False)  # sort_keys是否根据字母改变顺序\n",
    "        f.close()\n",
    "\n",
    "\n",
    "def yaml_update():\n",
    "    global yaml_data\n",
    "    yamlfile = path_yaml\n",
    "    f = open(yamlfile, \"r+\", encoding='utf-8')\n",
    "    yaml_data = yaml.load(f, Loader=yaml.FullLoader)\n",
    "    yaml_title[\"time\"][\"edit_time\"] = datetime.datetime.now().__str__()\n",
    "\n",
    "    # 更新图片文件夹\n",
    "    dirs = yaml_update_dir(path_sexypict, yaml_data)\n",
    "    for _dir in dirs:\n",
    "        yaml_update_dir_file(_dir, yaml_data)\n",
    "\n",
    "    f.seek(0)\n",
    "    yaml.dump(yaml_data, f, allow_unicode=True, sort_keys=False)  # sort_keys 是否根据字母改变顺序\n",
    "    f.close()\n",
    "\n",
    "\n",
    "def yaml_update_dir(_root_dir: Path, data: dict):\n",
    "    # 载入图片文件夹\n",
    "    ls = []\n",
    "    common_dir = []\n",
    "    special_dir = []\n",
    "    for i in os.listdir(_root_dir):\n",
    "        if os.path.isdir(path_sexypict.joinpath(i).resolve()):\n",
    "            ls.append(i)\n",
    "            if i.startswith(\"#\"):\n",
    "                special_dir.append(i)\n",
    "            else:\n",
    "                common_dir.append(i)\n",
    "    data[\"dir\"][\"common_dir\"] = common_dir\n",
    "    data[\"dir\"][\"special_dir\"] = special_dir\n",
    "    return ls\n",
    "\n",
    "\n",
    "def yaml_update_dir_file(_dir: Path, data: dict):\n",
    "    # 载入每个文件夹中的图片\n",
    "    pict_list = []\n",
    "    for i in os.listdir(path_sexypict.joinpath(_dir).resolve()):\n",
    "        if i.endswith(\".jpg\") or i.endswith(\"png\") or i.endswith(\".jpeg\"):\n",
    "            pict_list.append(i)\n",
    "    data[\"data\"][_dir] = pict_list\n",
    "\n",
    "\n",
    "def yaml_init():\n",
    "    yamlfile = path_yaml\n",
    "    if os.path.isfile(yamlfile):\n",
    "        print(\"找到yaml配置文件\")\n",
    "        #检查配置文件\n",
    "        with open(yamlfile, \"r\", encoding='utf-8') as f:\n",
    "            try:\n",
    "                data = yaml.load(f, Loader=yaml.FullLoader)\n",
    "                if data[\"__version__\"] == yaml_title[\"__version__\"]:\n",
    "                    print(\"版本号符合\")\n",
    "                else:\n",
    "                    print(\"版本号不符合\")\n",
    "                    yaml_create()\n",
    "            except:\n",
    "                print(\"读取版本号错误,重新配置\")\n",
    "                yaml_create()\n",
    "    else:\n",
    "        print(\"没有找到yaml配置文件，进行初始化配置\")\n",
    "        yaml_create()\n",
    "\n",
    "    yaml_update()\n",
    "\n",
    "\n",
    "def get_image(type_pict):\n",
    "    # 根据类型返回图片绝对路径\n",
    "    global yaml_data\n",
    "    if type_pict == \"common\":\n",
    "        n = random.choice(yaml_data[\"dir\"][\"common_dir\"])\n",
    "    if type_pict == \"special\":\n",
    "        n = random.choice(yaml_data[\"dir\"][\"common_dir\"])\n",
    "\n",
    "    if len(yaml_data[\"data\"][n]) is not 0:\n",
    "        nn = random.choice(yaml_data[\"data\"][n])\n",
    "        return path_sexypict.joinpath(n).joinpath(nn)\n",
    "    else:\n",
    "        return 0"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "主程序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 450,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "找到yaml配置文件\n",
      "版本号符合\n"
     ]
    }
   ],
   "source": [
    "if __name__ == \"__main__\":\n",
    "    yaml_init()\n",
    "    get_image(\"common\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "name": "python3",
   "language": "python",
   "display_name": "Python 3 (ipykernel)"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}